
import { ProductListForAdminOutDto, EntityDto } from '@shared/service-proxies/service-proxies';
import { Component, Injector, Inject, Optional, OpaqueToken, OnInit, ViewChild } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { AppComponentBase } from '@shared/app-component-base';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { ProductAddComponent } from '../product-add/product-add.component';
import { ProductInventorComponent } from '../product-inventor/product-inventor.component';
import { ModalDirective } from 'ngx-bootstrap';
import {
  API_BASE_URL,
  ProductListOutDto,
  ProductServiceProxy,
  PagedResultDtoOfProductListForAdminOutDto,
  ProductType,
  AddToInventoryInputDto,
  ProductListOutDtoStatus
} from '@shared/service-proxies/service-proxies';
import { AppConsts } from '@shared/AppConsts';
import { varToEnumStringVal } from '@shared/helpers/UrlHelper';

@Component({
  selector: 'product-list',
  templateUrl: 'product-list.component.html',
  styleUrls: ['product-list.component.scss'],
  animations: [appModuleAnimation()]
})
export class ProductListComponent extends AppComponentBase implements OnInit {

  @ViewChild('createProductModal') createProductModal: ProductAddComponent;
  @ViewChild('addToInventorModal') addToInventorModal: ProductInventorComponent;

  productList: PagedResultDtoOfProductListForAdminOutDto = new PagedResultDtoOfProductListForAdminOutDto();
  condition: GetProductsCondition = new GetProductsCondition();
  currentPage: number = 1;
  inventoryDto: AddToInventoryInputDto;
  downloadUrl: string = AppConsts.remoteServiceBaseUrl + '/file/download/';
  constructor(
    injector: Injector,
    private _productService: ProductServiceProxy,
    private route: ActivatedRoute
  ) {
    super(injector);
  }

  ngOnInit(): void {
    this.route.params.map(params => params['type']).subscribe((type) => {
      this.condition.productType = type;
      this.getProducts();
    });
  }

  getProducts(): void {

    this.condition.maxResultCount = 10;
    this._productService.getProductsForAdminAsync(this.condition.productType,
      this.condition.name,
      this.condition.categoryId,
      this.condition.maxResultCount,
      this.condition.skipCount,
      this.condition.sorting)
      .subscribe((result: PagedResultDtoOfProductListForAdminOutDto) => {
        this.productList = result;
      });
  }
  pageChanged($event: any): void {
    this.condition.skipCount = this.condition.maxResultCount * $event.page;
    this.getProducts();
  }
  createProduct(id?: number): void {
    this.createProductModal.show(this.condition.productType, id);
  }

  deleteProduct(id: number): void {
    abp.message.confirm('确定要删除该商品么?', '请确认!', (result: Boolean) => {
      if (result) {
        this._productService.deleteProductAsync(id).subscribe(() => {
          this.getProducts();
        });
      }
    });
  }


  isJewelry(productType: any): Boolean {
    let jewelry = ProductType.Jewelry;
    return varToEnumStringVal(productType, ProductType) === varToEnumStringVal(jewelry, ProductType);
  }
  addToInventor(id: number): void {
    this.addToInventorModal.show(id);
  }

  getStatusText(status: any): string {
    return varToEnumStringVal(status, ProductListOutDtoStatus);
  }

  toggleTheShelf(product: ProductListForAdminOutDto): void {

    var entityDto = new EntityDto();
    entityDto.id = product.id;
    if (product.status === 0) {
      abp.message.confirm("确定要下架此商品？", "请确认", (result: boolean) => {
        if (result) {
          this._productService.offTheShelfAsync(entityDto).subscribe(() => {
            this.getProducts();
          });
        }
      });
    } else {
      this._productService.onTheShelfAsync(entityDto).subscribe(() => {
        this.getProducts();
      });
    }
  }
}


class GetProductsCondition {
  productType: ProductType;
  name: string;
  categoryId: number;
  sorting: string;
  skipCount: number;
  maxResultCount: number;
};
